package com.shanxia.codec.cmd;

import com.shanxia.codec.Decoder;
import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import static com.shanxia.codec.Decoder.getBytesAsUint16;

/**
 * 充电桩上报BMS信息
 * 直流充电桩上报 BMS 信息
 */
@Data
public class Cmd301 extends ProtocolDataBase {

    public static final int MIN_SIZE = 4;

    private int v1;
    private int v2;

    public Cmd301(){
        super.setCmd(301);
    }

    public static Cmd301 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd301 cmd = new Cmd301();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        //数据域长度
        int len = dataField.length;
        if (len < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = Decoder.getUint16(protocolDataBase.getDataField(), offset);
        offset += 2;
        cmd.v2 = Decoder.getUint16(protocolDataBase.getDataField(), offset);
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[MIN_SIZE];
        int offset = 0;
        getBytesAsUint16(data, offset, this.v1);
        offset += 2;
        getBytesAsUint16(data, offset, this.v2);
        return data;
    }
}
